package com.qf.userinformationsystem.handler;

import com.qf.userinformationsystem.pojo.LoginAndRegister;
import com.qf.userinformationsystem.service.LoginAndRegisterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class LoginAndRegisterHandler{
    @Resource
    LoginAndRegisterService loginAndRegisterService;

    @GetMapping("/login")
    public String checkLogin(String account, String password, HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().setAttribute("account",account);

        List<LoginAndRegister> loginAndRegisters = loginAndRegisterService.checkLogin(account, password);
        List<LoginAndRegister> loginAndRegisters1 = loginAndRegisterService.checkAccount(account);

        if(loginAndRegisters1.isEmpty()){
            return "X";
        }else{
            if(!loginAndRegisters.isEmpty()){
                return "Y";
            }else {
                return "N";
            }
        }
    }

    @PostMapping("register")
    public String checkRegister(String account,String password,String confirmPassword){
        System.out.println(account);
        List<LoginAndRegister> loginAndRegisters = loginAndRegisterService.checkAccount(account);
        if(!loginAndRegisters.isEmpty()){
            return "X";
        }else{
            int insert = loginAndRegisterService.insert(account, password, confirmPassword);
            return insert==1?"Y":"N";
        }
    }

}
